package com.activiti.z_six.service.impl;

import com.activiti.z_six.entity.taskAssignee.AssigneeUserEntity;
import com.activiti.z_six.entity.taskAssignee.FlowElementAttrs;
import com.activiti.z_six.entity.taskAssignee.OvProcessInstance;
import com.activiti.z_six.mapper.taskAssigneeMapper.AssigneeUserMapper;
import com.activiti.z_six.mapper.taskAssigneeMapper.FlowElementAttrsMapper;
import com.activiti.z_six.mapper.taskAssigneeMapper.OvProcessInstanceMapper;
import com.activiti.z_six.security.RedisUtils;
import com.activiti.z_six.service.AssigneeUserService;
import com.activiti.z_six.util.ResultRes;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class AssigneeUserServiceImpl implements AssigneeUserService {
    @Autowired
    private AssigneeUserMapper assigneeUserMapper;
    @Autowired
    private OvProcessInstanceMapper ovProcessInstanceMapper;
    @Autowired
    private FlowElementAttrsMapper flowElementAttrsMapper;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public List<AssigneeUserEntity> getAssigneeUserWithTaskKey(String usertaskid){
        List<AssigneeUserEntity> assigneeUserEntityList;
        String assigneeString ="";
        if(redisUtils.exists(usertaskid)){
            assigneeString=redisUtils.get(usertaskid).toString();
        }

        //如果redis中不存在
        if (SystemConfig.IsNullOrEmpty(assigneeString)) {
            assigneeUserEntityList=assigneeUserMapper.getAssigneeUserWithTaskKey(usertaskid);
        }
        else {
            assigneeUserEntityList=JSONObject.parseArray(assigneeString, AssigneeUserEntity.class);
            if(SystemConfig.IsNullOrEmpty(assigneeUserEntityList.get(0).getUsername())
            &&SystemConfig.IsNullOrEmpty(assigneeUserEntityList.get(0).getName())){
                assigneeUserEntityList=assigneeUserMapper.getAssigneeUserWithTaskKey(usertaskid);
            }
        }
        return assigneeUserEntityList;
    }
    @Override
    public FlowElementAttrs getFormElement(String process_key){
        //获取流程模版信息
        OvProcessInstance ovProcessInstance=ovProcessInstanceMapper.getProcessInsLastVersion(process_key);
        BpmnModel bpmnModel = repositoryService.getBpmnModel(ovProcessInstance.getId_());
        //获取流程信息
        Process process=bpmnModel.getProcesses().get(0);
        StartEvent startEvent=(StartEvent)process.getInitialFlowElement();
        List<SequenceFlow> outgoingFlows = startEvent.getOutgoingFlows();
        //获取UserTask类型数据
        UserTask userTask = (UserTask) outgoingFlows.get(0).getTargetFlowElement();
        FlowElementAttrs flowElementAttrs=flowElementAttrsMapper.getFlowElementAttrs(userTask.getId());
        return flowElementAttrs;
    }
    @Override
    public String setAssigneeUser(String assigneeList, String usertaskid){
        JSONArray userlist = JSONObject.parseArray(assigneeList);
        List<AssigneeUserEntity> assigneeUserEntityList = new ArrayList<>();
        for (int i = 0; i < userlist.size(); i++) {
            JSONObject json = JSONObject.parseObject(JSON.toJSONString(userlist.get(i)));
            AssigneeUserEntity assigneeUserEntity = new AssigneeUserEntity();
            if(SystemConfig.IsNullOrEmpty(json.getString("username"))){
                assigneeUserEntity.setUsername(json.getString("id"));
            }
            else {
                assigneeUserEntity.setUsername(json.getString("username"));
            }
            assigneeUserEntity.setUsertaskid(usertaskid);
            assigneeUserEntity.setId(UUID.randomUUID().toString());
            assigneeUserEntityList.add(assigneeUserEntity);
        }
        assigneeUserMapper.setAssigneeUser(assigneeUserEntityList);

        return  "执行成功";
    }
    @Override
    public String deleteAssigneeUser(String usertaskid, String username){
        AssigneeUserEntity assigneeUserEntity=new AssigneeUserEntity();
        assigneeUserEntity.setUsertaskid(usertaskid);
        assigneeUserEntity.setUsername(username);
        assigneeUserMapper.deleteAssigneeUser(assigneeUserEntity);

        return "执行成功";
    }
    @Override
    public String deleteAssigneeUserByTaskid(String usertaskid){
        assigneeUserMapper.deleteAssigneeUserByTaskid(usertaskid);
        return "执行成功";
    }
}
